package com.JUC.cha03.sec03;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Runnable 和 Callable 的区别
 * 返回值  Runnable无   Callable有
 * 异步最好有返回值，故用Callable
 */
public class CompletableFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建异步任务
        FutureTask<String> futureTask = new FutureTask<>(new MyThread());
        // 创建新的线程，将异步任务给新的线程
        Thread t1 = new Thread(futureTask, "t1");
        // 线程就绪运行
        t1.start();
        // 获取异步任务执行结果
        System.out.println(futureTask.get());

    }

/*class MyThread implements Runnable{

        @Override
        public void run() {

        }
    }*/

    /**
     * 异步任务
     */
    static class MyThread implements Callable<String>{

    @Override
    public String call() throws Exception {
        System.out.println("------ come in call()");
        return "hello Call";
    }
}



}
